package com.itheima.oldTime;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Date;

/**
 * @ClassName: SimpleDateFormatUnSafeDemo
 * @Description: 创建10个线程，将字符串"2018-12-12 12:12:12"
 * 转换为Date对象后打印靠控制台上.
 * @author: Yrd
 * @date: 2021-09-27 23:31
 */
public class SimpleDateFormatUnSafeDemo {

    final static SimpleDateFormat SIMPLE_DATE_FORMAT =
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    final static DateTimeFormatter DATE_TIME_FORMATTER =
            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public static void main(String[] args){

        //testSimpleDateFormatError1();
        //testSimpleDateFormatError();
        //testSimpleDateFormat();
        testDateTimeFormatter();
    }

    public static void testSimpleDateFormatError1(){
        //循环10次，创建10个线程对象并启动
        Date date = new Date();
        for(int i=0;i<10;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {//这里不报错
                        String format = SIMPLE_DATE_FORMAT.format(date);
                        System.out.println(format);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }

    public static void testSimpleDateFormatError(){
        //循环10次，创建10个线程对象并启动
        for(int i=0;i<10;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {//这里必须加synchronized同步代码块，不然报错
                            Date date = SIMPLE_DATE_FORMAT.parse("2017-12-12 12:12:12");
                            System.out.println(date);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }

    public static void testSimpleDateFormat(){
        //循环10次，创建10个线程对象并启动
        for(int i=0;i<10;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {//这里必须加synchronized同步代码块，不然报错
                        synchronized (SIMPLE_DATE_FORMAT){
                            Date date = SIMPLE_DATE_FORMAT.parse("2018-12-12 12:12:12");
                            System.out.println(date);
                        }
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }

    /**
     *
     * @Title : testDateTimeFormatter
     * @Description : 线程安全

     * @return void
     *
     * @user : Yrd
     * @date : 2021-10-24 12:19
     */
    public static void testDateTimeFormatter(){
        //循环10次，创建10个线程对象并启动
        for(int i=0;i<10;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        //LocalDateTime parseDateTime = LocalDateTime.parse("2019-12-12 12:12:12", DATE_TIME_FORMATTER);
                        //System.out.println(parseDateTime);
                        //String format = DATE_TIME_FORMATTER.format(parseDateTime);
                        TemporalAccessor parse = DATE_TIME_FORMATTER.parse("2019-12-12 12:12:12");
                        System.out.println(parse.toString());//{},ISO resolved to 2019-12-12T12:12:12
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
}
